package com.tj.ocr.similar;

import java.awt.Color;
import java.awt.image.BufferedImage;

import com.tj.ocr.img.MyImage;

@SuppressWarnings("unused")
public class Project {
	int greenBins = 151;
	int blueBins = 28;
	int redBins = 77;
	static final int blank = Color.BLACK.getRGB();;

	public static boolean isBlank(int rgb){
		return rgb == blank;
		/*int ta = (rgb) & 0xff000000;
		int tr = (rgb >> 16) & 0xff;
		int tg = (rgb >> 8) & 0xff;
		int tb = rgb & 0xff;
		int gray = (tr * 77 + tg * 151 + tb * 28) >> 8;
		if(gray < 70){
			return true;
		}
		return false;*/
	}

    public static int[] filter(BufferedImage src) {
		int width = src.getWidth();
		int height = src.getHeight();
		int index = 0;
		
		//get binArray
		//int[] inPixels = new int[width * height];
		//src.getData().getDataBuffer().get
		//getRGB(src, 0, 0, width, height, inPixels);
		
		int[] rst = new int[width+height];
		for (int row = 0; row < height; row++) {
			int ta = 0, tr = 0, tg = 0, tb = 0;
			for (int col = 0; col < width; col++) {
				index = row * width + col;
				int rgb = src.getRGB(col, row);
				ta = (rgb) & 0xff000000;
				tr = (rgb >> 16) & 0xff;
				tg = (rgb >> 8) & 0xff;
				tb = rgb & 0xff;
				int gray = (tr * 77 + tg * 151 + tb * 28) >> 8;
				if(gray < 100){
					rst[row] ++;
					rst[height+col] ++;
				}
			}
		}

		/*// start to normalize the histogram data
		for (int i = 0; i < rst.length; i++) {
			rst[i] = rst[i] / total;
		}*/

		return rst;
	}

	public static int[] filter(BufferedImage img, int startx, int starty, int width, int height) {

		int index = 0;
		
		//get binArray
		//int[] inPixels = new int[width * height];
		//src.getData().getDataBuffer().get
		//getRGB(src, 0, 0, width, height, inPixels);
		
		int[] rst = new int[width+height];
		for (int row = starty; row < height+starty; row++) {
			int ta = 0, tr = 0, tg = 0, tb = 0;
			for (int col = startx; col < width+startx; col++) {
				index = (row-starty) * width + col - startx;
				int rgb = img.getRGB(col, row);				
				ta = (rgb) & 0xff000000;
				tr = (rgb >> 16) & 0xff;
				tg = (rgb >> 8) & 0xff;
				tb = rgb & 0xff;
				int gray = (tr * 77 + tg * 151 + tb * 28) >> 8;
				if(gray < 100){
					rst[row-starty] ++;
					rst[height+col-startx] ++;
				}
			}
		}

		/*// start to normalize the histogram data
		for (int i = 0; i < rst.length; i++) {
			rst[i] = rst[i] / total;
		}*/

		return rst;
	}
	
	public static boolean hasData(BufferedImage img,int x,int y) {
		//System.out.println(img.getRGB(x, y) );
		
		return isBlank(img.getRGB(x, y));
	}

	public static int[] filter(MyImage src) {
		int width = src.getWidth(), fromx = src.getFromx();
		int height = src.getHeight(), fromy = src.getFromy();
		int[][] data = src.getData();
		
		int[] rst = new int[width+height];
		for (int row = fromy; row < fromy +height; row++) {
			for (int col = fromx; col < fromx +width; col++) {
				if(data[col][row] == 1){
					rst[row-fromy] ++;
					rst[height+col-fromx] ++;
				}
			}
		}

		return rst;
	}
}
